package com.example.adminserivce.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adminserivce.Mapper.UserMapper;
import com.example.adminserivce.Service.UserService;
import com.example.common.constants.RdsConstants;
import com.example.common.constants.RoleConstants;
import com.example.common.domain.pojo.Teacher;
import com.example.common.domain.pojo.User;
import com.example.common.utils.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private CacheClient cacheClient;
    @Override
    public Long getTotalTeacherCount() {
        // 使用MyBatis-Plus的QueryWrapper构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", RoleConstants.TEACHER); // 等同于 SQL: WHERE role = 'teacher'

        // 调用MyBatis-Plus自带的count方法
        return this.count(queryWrapper);
    }

    @Override
    public Long getTotalStudentCount() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", RoleConstants.STUDENT); // 等同于 SQL: WHERE role = 'student'
        return this.count(queryWrapper);
    }

    @Override
    public User getUserByIdWithLogicalExpire(String id) {
        // 使用缓存穿透策略获取信息
        return cacheClient.queryWithPassThrough(
                RdsConstants.USER_INFO,
                id,
                User.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                2,
                TimeUnit.MINUTES
        );
    }

}
